package com.ss.android.article.base.feature.search;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bytedance.common.utility.collection.WeakHandler;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.app.a.ArticleConstants;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.article.base.feature.app.browser.GetEntryItemHelper;
import com.ss.android.article.browser.R;
import com.ss.android.common.app.AbsFragment;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.dialog.AlertDialog;
import com.ss.android.newmedia.a.DialogListener;
import com.ss.android.newmedia.a.WeakDialogListener;
import com.ss.android.newmedia.a.BrowserFragment;
import com.ss.android.newmedia.util.AppUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.json.JSONException;
import org.json.JSONObject;

import inline.SearchDemo;

public class a extends AbsFragment implements WeakHandler.Callback, u.b {
    public Context a;
    protected InputMethodManager b;
    public SSAutoCompleteTextView c;
    protected ImageView d;
    protected TextView e;
    public ImageView f;
    public String g;
    public String h;
    protected String i;
    protected String j;
    protected String k;
    public String l;
    protected long m;
    protected long n;
    protected int o;
    private WeakHandler p;
    private u q;
    private TextView r;
    private View s;
    private TextView t;
    private TextView u;
    private RelativeLayout v;
    private boolean w;
    private String x;
    private DialogListener y;

    public a() {
        this.p = new WeakHandler(Looper.getMainLooper(), ((WeakHandler.Callback)this));
        this.w = true;
        this.g = null;
        this.x = null;
        this.h = null;
        this.i = null;
        this.j = null;
    }

    public void a(String arg6, boolean arg7) {
        int v0 = 1;
        this.c.dismissDropDown();
        if(android.support.design.a.isTextEmpty(arg6)) {
            arg6 = this.c.getText().toString().trim();
        }

        if(arg6.length() <= 0) {
            if((n()) && (this.i.equals(this.c.getHint().toString()))) {
                this.c.setText(this.i);
                this.c.setSelection(this.i.length());
                this.b("tuijianci_search");
                this.a(this.i, true);
                return;
            }

            if(!arg7) {
                return;
            }

            this.d();
        }
        else {
            this.h();
            this.b("input_keyword_search");
            if("detail".equals(this.h)) {
                MobClickCombiner.onEvent(this.a, "search", "click_search_detail_icon");
            }

            SearchDemo.b();
            if(SearchDemo.e == 3) {
                SearchDemo.b();
                if(SearchDemo.f != 1) {
                    v0 = 0;
                }

                if(v0 != 0) {
                    MobClickCombiner.onEvent(this.getActivity(), "search_tab", "top_bar_bd_search");
                }else {
                    MobClickCombiner.onEvent(this.getActivity(), "search_tab", "top_bar_tt_search");
                }
            }

            this.b.hideSoftInputFromWindow(this.c.getWindowToken(), 0);
            this.a(arg6);
        }
    }

    public String a() {
        char v1_2;
        StringBuilder v3;
        StringBuilder v2;
        String v0 = null;
        if(!android.support.design.a.isTextEmpty(this.l)) {
            try {
                v2 = new StringBuilder(String.format(ArticleConstants.apiWapSearchUrl, this.h, URLEncoder.encode(this.l, "UTF-8")));
                AppUtil.a(v2);
                String v1_1 = v2.indexOf("#") > 0 ? "&" : "#";
                v2.append(v1_1);
                v3 = v2.append("tt_daymode=");
                AppData.getInstance();
                if(AppData.isNightMode()) {
                    v1_2 = '0';
                } else {
                    v1_2 = '1';
                }

                v3.append(v1_2);
                v0 = v2.toString();
            } catch(UnsupportedEncodingException v1) {
                v1.printStackTrace();
            }
        }

        return v0;
    }

    public final void a(String arg5) {
        if(!android.support.design.a.isTextEmpty(arg5)) {
            DBHelper.getInstance(this.a).a(this.c(), arg5, System.currentTimeMillis());
        }
    }

    public void b(String arg4) {
        String v0 = "search_tab";
        if("content".equals(this.h)) {
            v0 = "article_keyword_search";
        }
        else if("tag".equals(this.h)) {
            v0 = "article_tag_seach";
        }

        MobClickCombiner.onEvent(this.getActivity(), v0, arg4);
    }

    public int b() {
        return R.layout.base_search_fragment;
    }

    public int c() {
        return 0;
    }

    public final void c(String arg10) {
        long v4 = 0;
        if(!android.support.design.a.isTextEmpty(arg10)) {
            String v1 = this.c.getText().toString();
            JSONObject v8 = new JSONObject();
            try {
                v8.put("raw_query", v1);
                v8.put("click_query", arg10);
            }
            catch(JSONException v0) {
                v0.printStackTrace();
            }

            StringBuilder v2 = new StringBuilder("inputsug_");
            String v0_1 = v1 == null ? "0" : String.valueOf(v1.length());
            String v3 = v2.append(v0_1).toString();
            android.support.design.a.isTextEmpty(v3);
            MobClickCombiner.onEvent(this.getActivity(), "search_tab", v3, v4, v4, v8);
            this.a(arg10, true);
            this.c.setText(((CharSequence)arg10));
            SSAutoCompleteTextView v1_1 = this.c;
            int v0_2 = TextUtils.isEmpty(((CharSequence)arg10)) ? 0 : arg10.length();
            v1_1.setSelection(v0_2);
            this.c.dismissDropDown();
        }
    }

    protected final void d() {
        FragmentActivity v0 = this.getActivity();
        this.b.hideSoftInputFromWindow(this.c.getWindowToken(), 0);
        if((v0 instanceof p)) {
            ((p)v0).a();
        }
        else {
            this.getActivity().finish();
        }
    }

    public void d(String arg1) {
        this.b(arg1);
    }

    public void e() {
        boolean v0 = this.c.getText().toString().trim().length() > 0 ? true : false;
        this.d.setEnabled(v0);
        this.e.setEnabled(v0);
    }

    public void f() {
        if(!android.support.design.a.isTextEmpty(this.g)) {
            if("lite_sub_entr".equals(this.h)) {
                this.t.setText(this.g);
            }

            this.c.setText(this.g);
            this.l = this.g;
        }
        else {
            if(android.support.design.a.isTextEmpty(this.x)) {
                return;
            }

            this.c.setHint(this.x);
        }
    }

    protected final void g() {
        this.b("clear_input");
        this.c.setText("");
        this.b.showSoftInput(this.c, 0);
    }

    public void h() {
    }

    public void handleMsg(Message message) {
    }

    public final void i() {
        if(this.c != null) {
            this.c.setDropDownAlwaysVisiable(true);
        }

        AlertDialog.Builder v0 = com.ss.android.j.b.createDialogBuilder(this.getActivity());
        v0.setTitle(com.ss.android.article.browser.R.string.tip);
        v0.setMessage(com.ss.android.article.browser.R.string.tz);
        v0.setNegativeButton(com.ss.android.article.browser.R.string.cancel, new l(this));
        v0.setPositiveButton(com.ss.android.article.browser.R.string.rg, new com.ss.android.article.base.feature.search.c(this));
        AlertDialog v0_1 = v0.create();
        this.y = new com.ss.android.article.base.feature.search.d(this);
        v0_1.setOnDismissListener(new WeakDialogListener(this.y));
        v0_1.show();
    }

    protected void j() {
    }

    public void k() {
    }

    public void l() {
        this.c.dismissDropDown();
    }

    public BrowserFragment m() {
        return new GetEntryItemHelper();
    }

    protected static boolean n() {
        boolean v0 = true;
        if(AppData.getInstance().getAbSettings().getSearchRecommendNum() != 1 || !AppData.getInstance().getAbSettings().isWebSearchEnable()) {
            v0 = false;
        }

        return v0;
    }

    public void onActivityCreated(Bundle arg8) {
        int v6 = View.GONE;
        super.onActivityCreated(arg8);
        this.a = this.getActivity();
        this.g = null;
        this.h = null;
        this.m = 0;
        Bundle v0 = this.getArguments();
        if(v0 != null) {
            this.g = v0.getString("keyword");
            this.x = v0.getString("searchhint");
            this.h = v0.getString("from");
            this.i = v0.getString("homepage_search_suggest");
            v0.getString("init_from", "");
            this.j = v0.getString("init_category", "");
            this.k = this.h;
            this.m = v0.getLong("group_id");
            this.n = v0.getLong("item_id");
            this.o = v0.getInt("aggr_type");
            v0.getBoolean("new_arch", false);
            v0.getInt("enter_search_from", 0);
        }

        FragmentActivity v0_1 = this.getActivity();
        if("lite_sub_entr".equals(this.h)) {
            this.v.setVisibility(View.VISIBLE);
            this.s.setVisibility(v6);
            this.e.setVisibility(v6);
            this.r.setVisibility(v6);
            this.r = this.u;
        }

        if(this.c != null) {
            this.c.setIsLoading(false);
        }

        this.w = true;
        this.b = (InputMethodManager) ((Context)v0_1).getSystemService(Context.INPUT_METHOD_SERVICE);
        this.q = new com.ss.android.article.base.feature.search.u(this.a, this.c(), ((u.b)this));
        this.c.setAdapter(this.q);
        this.c.setThreshold(1);
        this.c.addTextChangedListener(new com.ss.android.article.base.feature.search.b(this));
        this.c.setOnEditorActionListener(new com.ss.android.article.base.feature.search.e(this));
        this.c.setOnClickListener(new f(this));
        this.c.setDropDownVerticalOffset(this.getResources().getDimensionPixelSize(R.dimen.ia));
        this.c.setDropDownWidth(this.getActivity().getWindowManager().getDefaultDisplay().getWidth());
        if((n()) && !TextUtils.isEmpty(this.i)) {
            this.c.setHint(this.i);
        }

        if(android.support.design.a.isTextEmpty(this.g)) {
            this.c.setFocusable(true);
            this.c.setFocusableInTouchMode(true);
            this.c.requestFocus();
            this.p.postDelayed(new g(this), 400);
        }

        this.d.setOnClickListener(new com.ss.android.article.base.feature.search.h(this));
        this.e.setOnClickListener(new i(this));
        this.f.setOnClickListener(new j(this));
        this.r.setOnClickListener(new k(this));
        Drawable v0_2 = this.a.getResources().getDrawable(com.ss.android.article.browser.R.drawable.detail_search_icon);
        if(v0_2 != null) {
            v0_2.setAlpha(102);
        }

        this.d.setImageDrawable(v0_2);
    }

    public void onActivityResult(int arg1, int arg2, Intent arg3) {
        super.onActivityResult(arg1, arg2, arg3);
    }

    public View onCreateView(LayoutInflater arg5, ViewGroup arg6, Bundle arg7) {
        int v3 = -1;
        AppData.getInstance();
        View v0 = arg5.inflate(this.b(), arg6, false);
        FrameLayout v1 = new FrameLayout(v0.getContext());
        ViewGroup.LayoutParams v2 = v0.getLayoutParams();
        if(v2 != null) {
            v1.setLayoutParams(v2);
        }

        v0.setLayoutParams(new FrameLayout.LayoutParams(v3, v3));
        v1.addView(v0);
        ((View)v1).findViewById(com.ss.android.article.browser.R.id.search_bg_layout);
        this.s = ((View)v1).findViewById(com.ss.android.article.browser.R.id.home_page_header_layout);
        this.r = ((View)v1).findViewById(com.ss.android.article.browser.R.id.mp);
        this.c = ((View)v1).findViewById(com.ss.android.article.browser.R.id.search_input);
        this.f = ((View)v1).findViewById(com.ss.android.article.browser.R.id.cancel_search);
        this.d = ((View)v1).findViewById(com.ss.android.article.browser.R.id.btn_search);
        this.e = ((View)v1).findViewById(com.ss.android.article.browser.R.id.mq);
        ((View)v1).findViewById(com.ss.android.article.browser.R.id.search_bottom_divide_line);
        this.t = ((View)v1).findViewById(com.ss.android.article.browser.R.id.mt);
        this.u = ((View)v1).findViewById(com.ss.android.article.browser.R.id.ms);
        this.v = ((View)v1).findViewById(com.ss.android.article.browser.R.id.mr);
        return ((View)v1);
    }

    public void onDestroy() {
        super.onDestroy();
    }

    public void onResume() {
        super.onResume();
        if(this.w) {
            this.b("enter");
            this.w = false;
        }
    }
}

